package com.lyh.controller;

import com.lyh.pojo.User;
import com.lyh.service.TicketService;
import com.lyh.utils.HtmlCode;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 退款
 */
@Controller
@RequestMapping("/refund")
public class RefundController {

    @Autowired
    TicketService ticketService;

    /**
     * 用户退款
     * @param orderId 订单号
     * @return 通知页面
     */
    @RequestMapping("/ticket/{orderId}")
    public String refund(@PathVariable("orderId") int orderId, Model model){

        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        int refundStatus = ticketService.refund(currentUser.getUserId(), orderId);
        //根据退款状态
        switch (refundStatus){
            case 2:
                HtmlCode.setMsg(model,"该订单或该用户号不存在!",2,"index");
                break;
            case 3:
                HtmlCode.setMsg(model,"退款时间已过!",2,"index");
                break;
            case 1:
                HtmlCode.setMsg(model,"退款成功!",1,"index");
                break;
            case 4:
                HtmlCode.setMsg(model, "该订单已经退款, 请勿重复退款!",2,"index");
                break;
            case 5:
                HtmlCode.setMsg(model, "该订单尚未付款, 不能退款!",2,"index");
                break;
        }
        return "msg";
    }

    /**
     * 用户尚未支付, 取消订单
     * @param orderId 需要取消的订单号
     */
    @RequestMapping("/cancel/{orderId}")
    public String cancel(@PathVariable("orderId")int orderId, Model model){
        User user = (User)SecurityUtils.getSubject().getPrincipal();
        int status = ticketService.cancel(user.getUserId(),orderId);
        switch (status){
            //设置提示的信息, 还有颜色状态, 还有跳转的URL
            case 0: HtmlCode.setMsg(model,"取消订单成功!",1,"index"); break;
            case 1: HtmlCode.setMsg(model,"该订单不存在!",2,"index"); break;
        }
        return "msg";
    }

}
